using System.Text.RegularExpressions;

namespace OneCaps.Core.Models
{
    /// <summary>
    /// 车牌号值对象
    /// </summary>
    public class LicensePlate
    {
        /// <summary>
        /// 车牌号
        /// </summary>
        public string Value { get; }

        public LicensePlate(string value)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                throw new ArgumentException("车牌号不能为空", nameof(value));
            }

            var normalizedValue = NormalizeLicensePlate(value);
            
            if (!IsValidLicensePlate(normalizedValue))
            {
                throw new ArgumentException($"无效的车牌号格式: {value}", nameof(value));
            }

            Value = normalizedValue;
        }

        /// <summary>
        /// 标准化车牌号
        /// </summary>
        private static string NormalizeLicensePlate(string value)
        {
            // 移除空格和特殊字符，转换为大写
            return Regex.Replace(value.Trim().ToUpper(), @"[^\w\u4e00-\u9fa5]", "");
        }

        /// <summary>
        /// 验证车牌号格式
        /// </summary>
        private static bool IsValidLicensePlate(string value)
        {
            // 普通车牌：京A12345
            var normalPattern = @"^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-Z0-9]{5}$";
            
            // 新能源车牌：京AD12345
            var newEnergyPattern = @"^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-Z0-9]{6}$";
            
            // 军车牌：军A12345
            var militaryPattern = @"^军[A-Z][0-9]{5}$";
            
            // 警车牌：警A12345
            var policePattern = @"^警[A-Z][0-9]{5}$";
            
            // 使馆车牌：使12345
            var embassyPattern = @"^使[0-9]{5}$";
            
            // 领事馆车牌：领12345
            var consulatePattern = @"^领[0-9]{5}$";

            return Regex.IsMatch(value, normalPattern) ||
                   Regex.IsMatch(value, newEnergyPattern) ||
                   Regex.IsMatch(value, militaryPattern) ||
                   Regex.IsMatch(value, policePattern) ||
                   Regex.IsMatch(value, embassyPattern) ||
                   Regex.IsMatch(value, consulatePattern);
        }

        /// <summary>
        /// 获取省份简称
        /// </summary>
        public string GetProvince()
        {
            if (Value.Length >= 2)
            {
                return Value.Substring(0, 2);
            }
            return string.Empty;
        }

        /// <summary>
        /// 获取车牌类型
        /// </summary>
        public new LicensePlateType GetType()
        {
            if (Value.StartsWith("军"))
                return LicensePlateType.Military;
            if (Value.StartsWith("警"))
                return LicensePlateType.Police;
            if (Value.StartsWith("使"))
                return LicensePlateType.Embassy;
            if (Value.StartsWith("领"))
                return LicensePlateType.Consulate;
            if (Value.Length == 8) // 新能源车牌
                return LicensePlateType.NewEnergy;
            return LicensePlateType.Normal;
        }

        /// <summary>
        /// 是否为新能源车牌
        /// </summary>
        public bool IsNewEnergy()
        {
            return GetType() == LicensePlateType.NewEnergy;
        }

        /// <summary>
        /// 是否为特殊车牌
        /// </summary>
        public bool IsSpecial()
        {
            var type = GetType();
            return type == LicensePlateType.Military || 
                   type == LicensePlateType.Police || 
                   type == LicensePlateType.Embassy || 
                   type == LicensePlateType.Consulate;
        }

        // 重写方法
        public override bool Equals(object? obj)
        {
            if (obj is not LicensePlate other)
                return false;

            return Value == other.Value;
        }

        public override int GetHashCode()
        {
            return Value.GetHashCode();
        }

        public override string ToString()
        {
            return Value;
        }

        // 运算符重载
        public static bool operator ==(LicensePlate left, LicensePlate right)
        {
            return left.Equals(right);
        }

        public static bool operator !=(LicensePlate left, LicensePlate right)
        {
            return !left.Equals(right);
        }

        // 隐式转换
        public static implicit operator string(LicensePlate licensePlate)
        {
            return licensePlate.Value;
        }
    }

    /// <summary>
    /// 车牌类型
    /// </summary>
    public enum LicensePlateType
    {
        /// <summary>
        /// 普通车牌
        /// </summary>
        Normal = 1,

        /// <summary>
        /// 新能源车牌
        /// </summary>
        NewEnergy = 2,

        /// <summary>
        /// 军车牌
        /// </summary>
        Military = 3,

        /// <summary>
        /// 警车牌
        /// </summary>
        Police = 4,

        /// <summary>
        /// 使馆车牌
        /// </summary>
        Embassy = 5,

        /// <summary>
        /// 领事馆车牌
        /// </summary>
        Consulate = 6
    }
} 